/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) chaitanya chandel 2009 <cchandel@yahoo.com>
 * 
 * main.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <Elementary.h>
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include "dbus-stuff.h"
#include "gui.h"
#include "callbacks.h"
#include "dbsqlite.h"

//globals
Ecore_X_Window my_win, focus_win;
Ecore_Timer *timer = NULL, *timer1 = NULL;
Evas_Object *win, *ic_sms, *ic_calls, *sms_lb, *call_lb, *toolbar, *ic_noti, *log_list;
char icon[200][255], path[200][255], name[200][255], launcher_dir[255], wall_path[255], cur_cat[255];
char icon_home[8][255], path_home[8][255], name_home[8][255];
char app_sms[255], app_calls[255], f_date[255], f_time[255], loc[255], cb_subs[255], con_path[255], hs_add[255];
char noti_app[20][255], noti_mess[20][255], noti_launch[20][255], national_prefix[10], country_code[10], area_code[10];
int noti_counter, int_log=0, int_sms=0, int_con=1, no_apps=0, show_c_no=0, reconnect_bt=0, pidof_btd=0;
int  brightness=0, GSM_st=1, GPS_st=0, WiFi_st=0, bt_st=0;
double  s_ts=0.0, c_ts=0.0;
_dir dir_gl_entry[255];
Ecore_File_Monitor *fm;
pthread_t thread_id;
_Cat *Cat;
_Log Log[30];
_conv *conv;
_contact *Contact;
_con_sms *Con_Sms;
_act_data act_da;

static void
my_win_del(void *data, Evas_Object *obj, void *event_info)
{
	save_state();
	e_dbus_connection_close(conn);
	sqlite3_close(launcher);
	sqlite3_close(launcher_thread);
	elm_exit();
}

void monitor_apps(void)
{
	fm = ecore_file_monitor_add("/usr/share/applications", refresh_app_data, NULL);
	if (fm == NULL) printf("failed to set monitor for directory\n");
}

#ifndef ELM_LIB_QUICKLAUNCH
EAPI int
elm_main(int argc, char **argv)
{
	//int ret;
	FILE *fp;
	char line[10], *ans;
	
	//restore state
	printf("restoring state\n");
	restore_state();
	get_opimd_data();
	
	//get pidof bluetoothd
	fp = popen("pidof bluetoothd", "r");
	ans = fgets(line, sizeof line, fp);
	pclose(fp);
	pidof_btd = atoi(line);
	
	//adjust finger size
	//elm_finger_size_set(60);
	
	//set up win
	printf("showing window\n");
	win = elm_win_add(NULL, "Launcher", ELM_WIN_BASIC);
	elm_win_title_set(win, "Please Wait...");
	evas_object_smart_callback_add(win, "delete-request", my_win_del, NULL);
	
	// show the window
	create_gui(win);
	evas_object_show(win);

	//init dbus and get opim data
	init_dbus_stuff();
	
	//set up directory monitoring
	monitor_apps();

	//show notification
	strcpy(noti_app[0], "Test");
	strcpy(noti_mess[0], "Call Roy");
	strcpy(noti_launch[0], "intone");
	
	elm_run();
	elm_shutdown();
	return 0;
}
#endif
ELM_MAIN()
